package cn.ekuma.epos.analysis.front;

import java.util.Date;
import java.util.List;
import javax.swing.table.TableModel;

import net.sourceforge.jtimepiece.TimeUtil;

import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Week;
import org.jfree.data.xy.XYDataset;

import cn.ekuma.epos.analysis.front.model.PassengerFlowAnalysisTableModel;
import cn.ekuma.epos.analysis.front.model.PassengerFlowTransactionTableModel;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.util.AbstractTimeGroupCalc;

import com.openbravo.bean.erp.viewbean.PassengerFlowAnalysisInfo;
import com.openbravo.bean.erp.viewbean.PassengerFlowTransaction;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFCompareEnum;

public class AnalysisModelManager {
	I_DataLogicERP dlSales;
	Object afilter;
	PassengerFlowAnalysisTableModel tableModel;
	AbstractTimeGroupCalc<PassengerFlowTransaction,PassengerFlowTransaction> groupCalc;
	List<PassengerFlowTransaction> transactionList;
	private TimeSeriesCollection valueTimeSeries;//量
	PassengerFlowTransactionTableModel flowTransactionTableModel;
	
	public AnalysisModelManager(I_DataLogicERP dlSales) {
        this.dlSales=dlSales;
        tableModel=new PassengerFlowAnalysisTableModel();
        flowTransactionTableModel=new PassengerFlowTransactionTableModel();
        valueTimeSeries=new TimeSeriesCollection();
        groupCalc=new AbstractTimeGroupCalc<PassengerFlowTransaction,PassengerFlowTransaction>(){

			@Override
			public PassengerFlowTransaction createNew(
					PassengerFlowTransaction inObj,Date date) {
				PassengerFlowTransaction cloneObj=new PassengerFlowTransaction();
				cloneObj.setConsultCost(inObj.getConsultCost());
				cloneObj.setCurDate(date);
				cloneObj.setTotalPrice(inObj.getTotalPrice());
				cloneObj.setUnit(inObj.getUnit());
				return cloneObj;
			}

			@Override
			public Date getKey(PassengerFlowTransaction inObj) {
				return inObj.getCurDate();
			}

			@Override
			public void op(PassengerFlowTransaction total,
					PassengerFlowTransaction inObj) {
				total.setConsultCost(total.getConsultCost()+inObj.getConsultCost());
				total.setTotalPrice(total.getTotalPrice()+inObj.getTotalPrice());
				total.setUnit(total.getUnit()+1);
			}
        	
        };
	}
	
	 public void query(Object filter) throws BasicException{
		 afilter=(Object[])filter;
         tableModel.resert(dlSales.listByFilter(PassengerFlowAnalysisInfo.class, afilter));
	 }
	 
	public TableModel getTableModel() {
		return tableModel;
	}
	
	public TimeSeriesCollection getValueTimeSeries() {
        return valueTimeSeries;
    }
	
	public XYDataset createValueDataset(int timeType){
		 List<PassengerFlowTransaction>  retList=groupCalc.calc(transactionList, timeType);
		 valueTimeSeries.removeAllSeries();
		 Class type=Hour.class;
		 switch(timeType){
    	 case 0:
    		 type=Hour.class;
    		 break;
    	 case 1:
    		 type=Day.class;
    		 break;
    	 case 2:
    		 type=Week.class;
    		 break;
    	 case 3:
    		 type=Month.class;
    		 break;
    	 case 4:
    		 type=Hour.class;
    	 }
         TimeSeries orderTypeTimeSerie=new TimeSeries("", type);
         for(PassengerFlowTransaction line:retList)
        	 switch(timeType){
        	 case 0:
        		 orderTypeTimeSerie.add(new Hour(line.getCurDate()),line.getTotalPrice());
        		 break;
        	 case 1:
        		 orderTypeTimeSerie.add(new Day(line.getCurDate()),line.getTotalPrice());
        		 break;
        	 case 2:
        		 orderTypeTimeSerie.add(new Week(line.getCurDate()),line.getTotalPrice());
        		 break;
        	 case 3:
        		 orderTypeTimeSerie.add(new Month(line.getCurDate()),line.getTotalPrice());
        		 break;
        	 case 4:
        		 orderTypeTimeSerie.add(new Hour(line.getCurDate()),line.getTotalPrice());
        	 }
                
         valueTimeSeries.addSeries(orderTypeTimeSerie);  
         flowTransactionTableModel.resert(retList);
       return valueTimeSeries;
   }

    public void fireProductChanged(PassengerFlowAnalysisInfo obj) throws BasicException {
	     Object[] tranFilter=new Object[12];
	     System.arraycopy(afilter, 0, tranFilter, 0, 6);
	     
	     tranFilter[6] = QBFCompareEnum.COMP_EQUALS;
	     tranFilter[7] = obj.getHostName();
	     
	     tranFilter[8] = QBFCompareEnum.COMP_EQUALS;
	     tranFilter[9] = obj.getLocation();
	     
	     tranFilter[10] = QBFCompareEnum.COMP_NONE;
	     tranFilter[11] = null;
	     
	     transactionList=dlSales.listByFilter(PassengerFlowTransaction.class, tranFilter);
	     createValueDataset(AbstractTimeGroupCalc.TIMERCYCLETYPE_TodayAndHour);
	     
    }
    
    
    public PassengerFlowTransactionTableModel getPassengerFlowTransactionTableModel(){
    	return flowTransactionTableModel;
    }
    
    
}
